/*----------------------------------------------------------------------------------------------------------*/
/*                                                                                 */
/* Copyright by huang ying xin                                                          */
/* All rights reserved.                                                                                     */
/*                                                                                                          */
/*------------------------------------------------------------------------------------------------------------
  File Name        : tsc2007.c
  Version          : V0.1
  Date[mm/dd/yyyy] : 06/11/2016
  Description      : Touch funciton driver.
------------------------------------------------------------------------------------------------------------*/

/* Includes ------------------------------------------------------------------------------------------------*/
#include "tsc2007.h" 
#include "gd32h7xx_i2c.h"
#include "i2c.h"
#include <stdio.h>

uint16_t z1_position;
uint16_t dev_add;

/* Private typedef -----------------------------------------------------------------------------------------*/
/* Private define ------------------------------------------------------------------------------------------*/

#define TSC2007_ADDRESS        0x90//0x4A//

/* Private macro -------------------------------------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------------------------------------*/
/* Private functions ---------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------------------
  Function Name  : tsc2007_Init
  Description    : Initializes peripherals used by the TSC2007 driver.
  Input          : None
  Output         : None
  Return         : None
------------------------------------------------------------------------------------------------------------*/
void tsc2007_Init(void)
{
	i2c_gpio_config(); 
	i2c_config();
	dev_add = TSC2007_ADDRESS;
}

/*!
    \brief      write one byte to the I2C bus
    \param[out] write_byte: byte to write
    \retval     none
*/
//void i2c_write_byte(uint8_t write_byte)
//{
//	/* configure slave address */
//	i2c_master_addressing(I2CX, dev_add, I2C_MASTER_TRANSMIT);
//	/* configure number of bytes to be transferred */
//	i2c_transfer_byte_number_config(I2CX, 1);
//	/* wait until I2C bus is idle */
//	while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY));
//	/* send a start condition to I2C bus */
//	i2c_start_on_bus(I2CX);
//	/* wait until the transmit data buffer is empty */
//	I2C_STAT(I2CX) |= I2C_STAT_TBE;
//	while(!i2c_flag_get(I2CX, I2C_FLAG_TBE));

//	/* data transmission */
//	i2c_data_transmit(I2CX, write_byte);
//	/* wait until the TI bit is set */
//	while(!i2c_flag_get(I2CX, I2C_FLAG_TI));
//	/* wait for transfer complete flag */
//	while(!i2c_flag_get(I2CX, I2C_FLAG_TC));
//	/* send a stop condition to I2C bus */
//	i2c_stop_on_bus(I2CX);
//	/* wait until stop condition generate */
//	while(!i2c_flag_get(I2CX, I2C_FLAG_STPDET));
//	/* clear the STPDET bit */
//	i2c_flag_clear(I2CX, I2C_FLAG_STPDET);

//}

/*!
    \brief      read data from the I2C bus
    \param[in]  p_buffer: pointer to the buffer that receives the data read from
    \param[in]  number_of_byte: number of bytes to reads from
    \param[out] none
    \retval     none
*/
//void i2c_read_bytes(uint8_t* p_buffer, uint16_t number_of_byte)
//{
//	uint16_t i;

//	/* configure I2C address */
////	i2c_address_config(I2CX, dev_add, I2C_ADDFORMAT_7BITS);
//	/* configure slave address */
//	i2c_master_addressing(I2CX, dev_add, I2C_MASTER_RECEIVE);
//	i2c_transfer_byte_number_config(I2CX, number_of_byte);
//	/* wait until I2C bus is idle */
//	while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY));
//	/* send a start condition to I2C bus */
//	i2c_start_on_bus(I2CX);
//	for(i = 0; i < number_of_byte; i++) 
//	{
//		/* wait until the RBNE bit is set */
//		while(!i2c_flag_get(I2CX, I2C_FLAG_RBNE));
//		/* read a data from I2C_DATA */
//		p_buffer[i] = i2c_data_receive(I2CX);
//	}
//	while(!i2c_flag_get(I2CX, I2C_FLAG_TC));
//	/* send a stop condition to I2C bus */
//	i2c_stop_on_bus(I2CX);
//	/* wait until stop condition generate */
//	while(!i2c_flag_get(I2CX, I2C_FLAG_STPDET));
//	/* clear the STPDET bit */
//	i2c_flag_clear(I2CX, I2C_FLAG_STPDET);

//}

/*------------------------------------------------------------------------------------------------------------
  Function Name  : tsc2007_MeasureXpos
  Description    : Measure the X position of touch point.
  Input          : -Mode: Conversion mode.                    
  Output         : The X position of touch point.
  Return         : None
------------------------------------------------------------------------------------------------------------*/
uint16_t tsc2007_MeasureXpos(void)
{
	uint8_t Xpos[2];
	uint16_t position;
	uint8_t command;
	command = Measure_X_Select | TSC2007_12_bit;
	
//	HAL_I2C_Master_Transmit(&heval_I2c, TSC2007_ADDRESS, &command, 1,100);
//	i2c_write_byte(command);
	i2c_write_bytes(dev_add, &command, 1);
		
//	HAL_I2C_Master_Receive(&heval_I2c, TSC2007_ADDRESS, Xpos, 2, 100);
//	i2c_read_bytes(Xpos,2);
	i2c_read_bytes(dev_add, &Xpos[0], 2);
	
	
	position = ((Xpos[0]<<4) | (Xpos[1]>>4));
	return position;
}

/*------------------------------------------------------------------------------------------------------------
  Function Name  : tsc2007_MeasureYpos
  Description    : Measure the Y position of touch point.
  Input          : -Mode: Conversion mode.                    
  Output         : The Y position of touch point.
  Return         : None
------------------------------------------------------------------------------------------------------------*/
uint16_t tsc2007_MeasureYpos(void)
{
	uint8_t Ypos[2];
	uint16_t position;
	uint8_t command;
	command = Measure_Y_Select | TSC2007_12_bit;
	
//	HAL_I2C_Master_Transmit(&heval_I2c, TSC2007_ADDRESS, &command, 1,100);
//	i2c_write_byte(command);
	i2c_write_bytes(dev_add, &command, 1);
	
//	HAL_I2C_Master_Receive(&heval_I2c, TSC2007_ADDRESS, Ypos, 2, 100);
//	i2c_read_bytes(Ypos,2);
	i2c_read_bytes(dev_add, &Ypos[0], 2);
	
	position = ((Ypos[0]<<4) | (Ypos[1]>>4));
	return position;
} 

/*------------------------------------------------------------------------------------------------------------
  Function Name  : tsc2007_MeasureZ1pos
  Description    : Measure the Z1 position of touch point.
  Input          : -Mode: Conversion mode.                    
  Output         : The Z1 position of touch point.
  Return         : None
------------------------------------------------------------------------------------------------------------*/
uint16_t tsc2007_MeasureZ1pos(void)
{
	uint8_t Z1pos[2];
	uint16_t position;
	uint8_t command;
	command = Measure_Z1_Select | TSC2007_12_bit;
	
//	HAL_I2C_Master_Transmit(&heval_I2c, TSC2007_ADDRESS, &command, 1,100);
//	i2c_write_byte(command);
	i2c_write_bytes(dev_add, &command, 1);
//	HAL_I2C_Master_Receive(&heval_I2c, TSC2007_ADDRESS, Z1pos, 2, 100);
//	i2c_read_bytes(Z1pos,2);
	i2c_read_bytes(dev_add, &Z1pos[0], 2);
	
	position = ((Z1pos[0]<<4) | (Z1pos[1]>>4));
	return position;
}

/*------------------------------------------------------------------------------------------------------------
  Function Name  : tsc2007_MeasureZ2pos
  Description    : Measure the Z2 position of touch point.
  Input          : -Mode: Conversion mode.                    
  Output         : The Z2 position of touch point.
  Return         : None
------------------------------------------------------------------------------------------------------------*/
uint16_t tsc2007_MeasureZ2pos(void)
{
	uint8_t Z2pos[2];
	uint16_t position;
	uint8_t command;
	command = Measure_Z2_Select | TSC2007_12_bit;
	
//	HAL_I2C_Master_Transmit(&heval_I2c, TSC2007_ADDRESS, &command, 1,100);
//	i2c_write_byte(command);
	i2c_write_bytes(dev_add, &command, 1);

//	HAL_I2C_Master_Receive(&heval_I2c, TSC2007_ADDRESS, Z2pos, 2, 100);
//	i2c_read_bytes(Z2pos,2);
	i2c_read_bytes(dev_add, &Z2pos[0], 2);
	
	position = ((Z2pos[0]<<4) | (Z2pos[1]>>4));
	return position;
}

/**
  * @brief  Return if there is touch detected or not.
  * @param  DeviceAddr: Device address on communication Bus.
  * @retval Touch detected state.
  */
uint8_t tsc2007_TS_DetectTouch()
{
  uint8_t ret = 0;
//	uint16_t z1_position;
	
	z1_position = tsc2007_MeasureZ1pos();
	if(z1_position > 0x000F)
		ret = 1;
	return ret;
}

